home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / Perl_WWW_Utilities / makehomeidx / makehomeidx < prev    next >
Encoding:
Text File  |  1996-01-02  |  6.7 KB  |  232 lines

  1. #! /usr/local/bin/perl
  2. ##---------------------------------------------------------------------------##
  3. ##  File:
  4. ##      makehomeidx
  5. ##  Author:
  6. ##      Earl Hood       ehood@convex.com
  7. ##  Description:
  8. ##    makehomeidx searches all entries /etc/passwd and creates a home page
  9. ##    index for a Web server of all users who have defined a home page
  10. ##    in their public HTML directory.
  11. ##---------------------------------------------------------------------------##
  12. ##  Copyright (C) 1994  Earl Hood, ehood@convex.com
  13. ##
  14. ##  This program is free software; you can redistribute it and/or modify
  15. ##  it under the terms of the GNU General Public License as published by
  16. ##  the Free Software Foundation; either version 2 of the License, or
  17. ##  (at your option) any later version.
  18. ##  
  19. ##  This program is distributed in the hope that it will be useful,
  20. ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ##  GNU General Public License for more details.
  23. ##  
  24. ##  You should have received a copy of the GNU General Public License
  25. ##  along with this program; if not, write to the Free Software
  26. ##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. ##---------------------------------------------------------------------------##
  28.  
  29. require "newgetopt.pl" || die "Unable to require newgetopt.pl\n";
  30.  
  31. ## Store name of program ##
  32. ($PROG = $0) =~ s/.*\///;
  33. $VERSION = '1.1.1';
  34.  
  35. %includes = ();
  36. %excludes = ();
  37.  
  38. ## Get command-line options ##
  39. &Usage() unless
  40. &NGetOpt(
  41.  
  42.     "excfile=s",    # Exclude file
  43.     "footer=s",        # Filename of footer text for home page index
  44.     "header=s",        # Filename of header text for home page index
  45.     "hostname=s",    # Hostname of machine
  46.     "idxrc=s",        # Name of indiv resource file
  47.     "incfile=s",    # Include file
  48.     "passwd=s",        # Password file to use
  49.     "pubhtml=s",    # Public html directory
  50.     "title=s",        # Title of home page index
  51.  
  52.     "help"
  53. );
  54. &Usage() if defined($opt_help);
  55. &Usage("No destination file") if $#ARGV < 0;
  56. &read_excfile($opt_excfile)  if $opt_excfile;
  57. &read_incfile($opt_incfile)  if $opt_incfile;
  58.  
  59. ## Initialize variables ##
  60. open(HOMEIDX, "> $ARGV[0]") || die "Unable to create $ARGV[0]\n";
  61. $H1 = ($opt_title ? $opt_title : "Home Pages");
  62. ($TITLE = $H1) =~ s/<[^>]*>//g;
  63. $HEADER = "";
  64. if ($opt_header) {
  65.     if (open(HEADER, "$opt_header")) {
  66.     while (<HEADER>) { $HEADER .= $_; }
  67.     } else {
  68.     warn "Unable to open $opt_header\n";
  69.     }
  70. }
  71. $FOOTER = "";
  72. if ($opt_footer) {
  73.     if (open(FOOTER, "$opt_footer")) {
  74.     while (<FOOTER>) { $FOOTER .= $_; }
  75.     } else {
  76.     warn "Unable to open $opt_footer\n";
  77.     }
  78. }
  79. $PUBHTML = ($opt_pubhtml ? $opt_pubhtml : "public_html");
  80. $IDXRC = ($opt_idxrc ? $opt_idxrc : ".homeidxrc");
  81. $HOSTNAME = ($opt_hostname ? $opt_hostname : `hostname`);
  82.     $HOSTNAME =~ s/\s//g;
  83. $PASSWD = ($opt_passwd ? $opt_passwd : "/etc/passwd");
  84.  
  85.  
  86. ## Start HTML of home page index ##
  87. if ($HEADER && ($HEADER =~ /<title/i)) {
  88.     print HOMEIDX $HEADER;
  89. } else {
  90.     print HOMEIDX "<html>\n",
  91.           "<head>\n",
  92.           "<title>$TITLE</title>\n",
  93.           "</head>\n",
  94.           "<body>\n",
  95.           "<h1>$H1</h1>\n",
  96.           "<hr>\n",
  97.           $HEADER, "\n";
  98. }
  99.  
  100. %Names = ();
  101. %Jobs = ();
  102. %HomePages = ();
  103.  
  104. ## Read passwd file ##
  105. if ($PASSWD eq '-') {
  106.     $PFile = 'STDIN';
  107. } else {
  108.     open(PASSWD, $PASSWD) || die "Can't open $PASSWD: $!\n";
  109.     $PFile = 'PASSWD';
  110. }
  111. while (<$PFile>) {
  112.     chop;
  113.     ($login,$passwd,$uid,$gid,$gcos,$home,$shell) = split(/:/);
  114.     next  if ($excludes{$login} || (defined($includes{$login}) &&
  115.                     !$includes{$login}));
  116.     ($name,$office,$phone,$misc) = split(/,/,$gcos);
  117.     $dir = "$home/$PUBHTML";
  118.     if (-d $dir && ($homepage = &get_home_name($dir, $login))) {
  119.     $job = "";
  120.     if (-f "$dir/$IDXRC" && open(IDXRC, "$dir/$IDXRC")) {
  121.         $name = <IDXRC>;  chop $name;
  122.         $job = <IDXRC>;  chop $job  if $job =~ /\n$/;
  123.         close(IDXRC);
  124.     }
  125.     if ($name) {
  126.         (@array) = split(' ', $name);
  127.         unshift(@array, pop @array) if $#array > 0;
  128.         $idx = join(' ', @array);
  129.     } else {
  130.         $idx = $login;
  131.     }
  132.     $Names{$idx} = $name;
  133.     $Jobs{$idx} = $job;
  134.     $HomePages{$idx} = $homepage;
  135.     }
  136. }
  137. close($PFile);
  138.  
  139. ## Write index ##
  140. print HOMEIDX "<ul>\n";
  141. foreach (sort keys %Names) {
  142.     print HOMEIDX qq|<li><a href="$HomePages{$_}">$Names{$_}</a>|;
  143.     print HOMEIDX qq| - $Jobs{$_}|  if ($Jobs{$_} !~ /^\s*$/);
  144.     print HOMEIDX qq|</li>\n|;
  145. }
  146. print HOMEIDX "</ul>\n";
  147.  
  148. if ($FOOTER && (($FOOTER =~ m%</body%i) || ($FOOTER =~ m%</html%i))) {
  149.     print HOMEIDX $FOOTER;
  150. } else {
  151.     print HOMEIDX $FOOTER, "\n",
  152.           "</body>\n",
  153.           "</html>\n";
  154. }
  155.  
  156. exit 0;
  157.  
  158. ##---------------------------------------------------------------------------
  159. sub get_home_name {
  160.     local($path, $login) = @_;
  161.     local($ret);
  162.  
  163.     if (-f "$path/index.html") {
  164.     $ret = "http://$HOSTNAME/%7E$login/index.html";
  165.     } elsif (-f "$path/home.html") {
  166.     $ret = "http://$HOSTNAME/%7E$login/home.html";
  167.     } elsif (-f "$path/$login.html") {
  168.     $ret = "http://$HOSTNAME/%7E$login/$login.html";
  169.     } else {
  170.     $ret = "";
  171.     }
  172.     $ret; 
  173. }
  174. ##---------------------------------------------------------------------------
  175. sub read_excfile {
  176.     local($filename) = shift;
  177.  
  178.     if (open(EXCFILE, $filename)) {
  179.     while (<EXCFILE>) {
  180.         s/\s//g;
  181.         $excludes{$_} = 1  if $_;
  182.     }
  183.     } else {
  184.     warn "Warning: Unable to open $filename\n";
  185.     }
  186. }
  187. ##---------------------------------------------------------------------------
  188. sub read_incfile {
  189.     local($filename) = shift;
  190.  
  191.     if (open(INCFILE, $filename)) {
  192.     while (<INCFILE>) {
  193.         s/\s//g;
  194.         $includes{$_} = 1  if $_;
  195.     }
  196.     } else {
  197.     warn "Warning: Unable to open $filename\n";
  198.     }
  199. }
  200. ##---------------------------------------------------------------------------
  201. sub Usage {
  202.     local($str) = shift;
  203.     print STDOUT $str if $str;
  204.     print STDOUT <<EndOfUsage;
  205. Usage: $PROG [<options>] directory ... 
  206. Options:
  207.   -excfile <filename>    : User exclude file
  208.   -footer <filename>    : Filename of footer text for home page index
  209.   -header <filename>    : Filename of header text for home page index
  210.   -help            : This message.
  211.   -hostname <string>    : Hostname of machine (def: `hostname`)
  212.   -idxrc <name>        : Name of indiv resource file (def: ".homeidxrc")
  213.   -incfile <filename>    : User include file
  214.   -passwd <filename>    : Password file to use (def: "/etc/passwd")
  215.   -pubhtml <name>    : Public html directory    name (def: "public_html")
  216.   -title <string>    : Title of home page index (def: "Home Pages")
  217. Description:
  218.   $PROG searches all entries /etc/passwd and creates a home page index
  219.   for a Web server of all users who have defined a home page in their public
  220.   HTML directory.
  221. Version:
  222.   $VERSION
  223.   Copyright (C) 1995  Earl Hood, ehood\@convex.com
  224.   $PROG comes with ABSOLUTELY NO WARRANTY and $PROG may be copied
  225.   only under the terms of the GNU General Public License, which may be found
  226.   in the $PROG distribution.
  227.  
  228. EndOfUsage
  229.  
  230.     exit 0;
  231. }
  232.